// 学习元信息
// vue-4.x：
import { createRouter, createWebHistory } from 'vue-router';

// const routes = [
//     {
//         path: "/",
//         name: "index",
//         component: () => import("../components/08router-meta/meta-index")
//     },
//     {
//         path: "/login",
//         name: "login",
//         component: () => import("../components/08router-meta/user-login")
//     },
//     {
//         path: "/my-center",
//         name: "my-center",
//         component: () => import("../components/08router-meta/my-center"),
//         meta: {requiredAuth: true}
//     },
//     {
//         path: "/my-cart",
//         name: "my-cart",
//         component: () => import("../components/08router-meta/my-cart"),
//         meta: {requiredAuth: true}
//     },
//     {
//         path: '/:pathMatch(.*)*',
//         redirect: '/'
//     }
// ]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes: []
});


// 采用动态路由来创建路由规则
router.addRoute({path: "/",name:"index",component: () => import("../components/08router-meta/meta-index")});
router.addRoute({path: "/login",name:"login",component: () => import("../components/08router-meta/user-login")});
router.addRoute({path: "/my-center",name:"my-center",meta: {requiredAuth: true},component: () => import("../components/08router-meta/my-center")});
router.addRoute({path: "/my-cart",name:"my-cart",meta: {requiredAuth: true},component: () => import("../components/08router-meta/my-cart")});
router.addRoute({path: "/:pathMatch(.*)*",redirect:"/"});

// 通过name删除路由
// router.removeRoute("my-center");


router.beforeEach((to) => {
    // 如果没有登录
    if (to.meta.requiredAuth) {
        if (!sessionStorage.getItem("isLogin")) {
            console.log("你没有登录")
            sessionStorage.setItem("path",to.fullPath);
            return {"path":"/login"};
        }
        return true;
    } 
    return true;
})

// 导出路由对象
export default router;